package cn.iocoder.yudao.module.pm.controller.admin.cooked;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pm.controller.admin.cooked.vo.*;
import cn.iocoder.yudao.module.pm.dal.dataobject.cooked.CookedDO;
import cn.iocoder.yudao.module.pm.service.cooked.CookedService;

@Tag(name = "管理后台 - 熟食成品管理")
@RestController
@RequestMapping("/pm/cooked")
@Validated
public class CookedController {

    @Resource
    private CookedService cookedService;

    @PostMapping("/create")
    @Operation(summary = "创建熟食成品管理")
    @PreAuthorize("@ss.hasPermission('pm:cooked:create')")
    public CommonResult<Long> createCooked(@Valid @RequestBody CookedSaveReqVO createReqVO) {
        return success(cookedService.createCooked(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新熟食成品管理")
    @PreAuthorize("@ss.hasPermission('pm:cooked:update')")
    public CommonResult<Boolean> updateCooked(@Valid @RequestBody CookedSaveReqVO updateReqVO) {
        cookedService.updateCooked(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除熟食成品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:cooked:delete')")
    public CommonResult<Boolean> deleteCooked(@RequestParam("id") Long id) {
        cookedService.deleteCooked(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除熟食成品管理")
                @PreAuthorize("@ss.hasPermission('pm:cooked:delete')")
    public CommonResult<Boolean> deleteCookedList(@RequestParam("ids") List<Long> ids) {
        cookedService.deleteCookedListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得熟食成品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:cooked:query')")
    public CommonResult<CookedRespVO> getCooked(@RequestParam("id") Long id) {
        CookedDO cooked = cookedService.getCooked(id);
        return success(BeanUtils.toBean(cooked, CookedRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得熟食成品管理分页")
    @PreAuthorize("@ss.hasPermission('pm:cooked:query')")
    public CommonResult<PageResult<CookedRespVO>> getCookedPage(@Valid CookedPageReqVO pageReqVO) {
        PageResult<CookedDO> pageResult = cookedService.getCookedPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CookedRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出熟食成品管理 Excel")
    @PreAuthorize("@ss.hasPermission('pm:cooked:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCookedExcel(@Valid CookedPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CookedDO> list = cookedService.getCookedPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "熟食成品管理.xls", "数据", CookedRespVO.class,
                        BeanUtils.toBean(list, CookedRespVO.class));
    }

}